var logger = require("pomelo-logger").getLogger(__filename);

module.exports = function (app) {
    return new Remote(app);
};

var Remote = function (app) {
    this.app = app;
};

Remote.prototype.getConfigByKey = function (msg, callback) {
    let configService = this.app.get("configService");
    if(!msg.hasOwnProperty("key")) {
        callback({resultCode: 404, message: "key为空"});
        return;
    }
    let config = configService.getConfigByKey(msg["key"]);
    if(!!config) {
        callback({
            resultCode: 0,
            message: "ok",
            config: config.toJSON()
        });
    }else{
        callback({
            resultCode: 404,
            message: "config为空.get"
        });
    }

};

Remote.prototype.getAllConfigs = function (msg, callback) {
    let configService = this.app.get("configService");
    callback({
        resultCode: 0,
        message: "ok",
        config: configService.toJSON()
    });
};

Remote.prototype.updateConfigValue = function (msg, callback) {
    let configService = this.app.get("configService");
    if(!msg.hasOwnProperty("key")) {
        callback({resultCode: 404, message: "key为空"});
        return;
    }
    if(!msg.hasOwnProperty("value")) {
        callback({resultCode: 404, message: "value为空"});
        return;
    }
    configService.updateConfigValue(msg.key, msg.value, function (key) {
        if(!!key) {
            callback({
                resultCode: 0,
                message: "ok",
                config: {key : key}
            });
        }else{
            callback({
                resultCode: 404,
                message: "config为空.update"
            });
        }
    });
};

Remote.prototype.updateConfigEnable = function (msg, callback) {
    let configService = this.app.get("configService");
    if(!msg.hasOwnProperty("key")) {
        callback({resultCode: 404, message: "key为空"});
        return;
    }
    if(!msg.hasOwnProperty("enable")) {
        callback({resultCode: 404, message: "enable为空"});
        return;
    }
    let enable = msg.enable === "true";
    configService.updateConfigEnable(msg.key, enable, function (key) {
        if(!!key) {
            callback({
                resultCode: 0,
                message: "ok",
                config: {key : key}
            });
        }else{
            callback({
                resultCode: 404,
                message: "config为空.update"
            });
        }
    });
};

Remote.prototype.addConfig = function (msg, callback) {
    let configService = this.app.get("configService");
    if(!msg.hasOwnProperty("key")) {
        callback({resultCode: 404, message: "key为空"});
        return;
    }
    if(!msg.hasOwnProperty("value")) {
        callback({resultCode: 404, message: "value为空"});
        return;
    }
    if(!msg.hasOwnProperty("desc")) {
        callback({resultCode: 404, message: "desc为空"});
        return;
    }
    if(!msg.hasOwnProperty("enable")) {
        callback({resultCode: 404, message: "enable为空"});
        return;
    }
    let enable = msg.enable === "true";
    configService.addConfig(msg.key, msg.value, msg.desc, enable, function (config) {
        if(!!config) {
            callback({
                resultCode: 0,
                message: "ok",
                config : config.toJSON()
            });
        }else{
            callback({
                resultCode: 404,
                message: "config已存在或者config为空.add"
            });
        }
    });
};

Remote.prototype.deleteConfigByKey = function (msg, callback) {
    let configService = this.app.get("configService");
    if(!msg.hasOwnProperty("key")) {
        callback({resultCode: 404, message: "key为空"});
        return;
    }
    configService.deleteConfigByKey(msg.key, function (key) {
        if(!!key) {
            callback({
                resultCode: 0,
                message: "ok",
                config : {key : key}
            });
        }else{
            callback({
                resultCode: 0,
                message: "config为空.delete"
            });
        }
    });
};

